package com.lichuandi.app;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * 客户端程序，每隔3秒获取一次最新的数据并打印
 * @author admin
 */
public class ClientApp {
    public static void main(String[] args) {
        HttpClient client = HttpClient.newHttpClient();
        String apiUrl = "http://localhost:8080/api/latest";

        while (true) {
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(apiUrl))
                    .GET()
                    .build();

            try {
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
                System.out.println("当前环境数据: " + response.body());
                // 每3秒刷新一次数据
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
